home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP3.TXT < prev    next >
Text File  |  1988-11-27  |  10KB  |  256 lines

  1.                  CHAPTER 3 - The simple Pascal data types
  2.  
  3.  
  4.             Pascal  has only 5 basic data types which are predefined
  5.         and  can be used anywhere you desire provided you  use  them
  6.         properly.   The  five  types  and a very  brief  description
  7.         follows;
  8.  
  9.             Integer   The integers from -32768 to 32767
  10.             Byte      The integers from 0 to 255
  11.             Real      Floating point numbers from 1E-38 to 1E+38
  12.             Boolean   Can only have the value TRUE or FALSE
  13.             Char      Any character in the ASCII character set
  14.  
  15.             Please note that the Byte type of data is not a part  of
  16.         the  standard  Pascal  definition  but  is  included  as  an
  17.         extension to the TURBO Pascal compiler.
  18.  
  19.             A  complete definition of these can be found on pages 41
  20.         and  42 of the TURBO Pascal reference manual (version  3.0).
  21.         It  would  be good to read those two pages now  for  a  good
  22.         definition  prior to our learning how to define and use them
  23.         in  a  program.   The  integers are by far  the  easiest  to
  24.         understand so we will start with a simple program that  uses
  25.         some  integers in a very simple way.   Load INTVAR into your
  26.         TURBO system and lets take a look at it.
  27.  
  28.                             OUR FIRST VARIABLES
  29.  
  30.             Immediately  following the PROGRAM statement is  another
  31.         reserved word, VAR.  VAR is used to define a variable before
  32.         it  can  be  used anywhere in  the  program.   There  is  an
  33.         unbroken  rule  of Pascal that states "Nothing can  be  used
  34.         until  it is defined."  The compiler will complain at you if
  35.         you try to use a variable without properly defining it.   It
  36.         seems  a  bit bothersome to have to  define  every  variable
  37.         prior  to  its  use but this rule will catch  many  spelling
  38.         errors of variables before they cause trouble.   Some  other
  39.         languages  will  simply define a new variable with  the  new
  40.         name and go merrily on its way producing some well formatted
  41.         garbage for you.
  42.  
  43.             Notice  that  there is only one VAR,  but it is used  to
  44.         define three different variables,  "count",  "x",  and  "y".
  45.         Once  a  VAR is recognized,  the compiler will  continue  to
  46.         recognize  variable  definitions  line after line  until  it
  47.         finds another reserved word.  It would be permissible to put
  48.         a VAR on the second line also but it is not  necessary.   It
  49.         would  also be permissible to put all three variables on one
  50.         line  but programming style will dictate where you  put  the
  51.         three  variables.   Following the colon on each line is  the
  52.         word  INTEGER  which  is  a  standard  identifier  which  is
  53.         different from a reserved word.  An identifier is predefined
  54.         like  a reserved word but you can redefine it thereby losing
  55.  
  56.  
  57.                                   Page 11
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  CHAPTER 3 - The simple Pascal data types
  68.  
  69.  
  70.         its  original purpose and meaning.   For now and for a  long
  71.         time, don't do that. Page 38 contains a list of identifiers.
  72.  
  73.                            OUR FIRST ARITHMETIC
  74.  
  75.             Now that we have three variables defined as integer,  we
  76.         are  free to use them in a program in any way we  desire  as
  77.         long as we use them properly.   If we tried to assign a REAL
  78.         value  to  "x",  the compiler will generate an  error,  once
  79.         again preventing a garbage output.  Observe the start of the
  80.         main  body  of  the program.   There  are  three  statements
  81.         assigning values to "x",  "y", and "count".  A fine point of
  82.         mathematics  would state that "count" is only equal  to  the
  83.         value of x+y until either was modified,  therefore the equal
  84.         sign used in so many other languages is not used here.   The
  85.         sign  :=  is used,  and can be read as "is replaced  by  the
  86.         value   of"   when  reading  a  listing  to   preserve   the
  87.         mathematical purity of Pascal. Another quicker way is to use
  88.         the word "gets".   Thus x := x + 1 would be read "x gets the
  89.         value of x plus 1".  We will see later that the simple equal
  90.         sign is reserved for use in a different manner.
  91.  
  92.             The first three statements give "x" the value of 12, "y"
  93.         the  value of 13,  and "count" the value of 12+13 or 25.  We
  94.         need  to get those values out of the computer,  so  we  need
  95.         another extension to the WRITELN statement.   The first part
  96.         of the data within the parentheses should be familiar to you
  97.         now,  but  the second part is new.   Multiple outputs can be
  98.         handled within one WRITELN if the fields are separated by  a
  99.         comma.   To  output a variable,  simply write the variable's
  100.         name in the output field.  The number following the variable
  101.         in  each case is the number of output columns to be used  by
  102.         the output data.  This number is optional and can be omitted
  103.         allowing the system to use as many columns as it needs.  For
  104.         purposes  of  illustration  they  have  all  been   assigned
  105.         different  numbers  of  columns.   At this  point,  you  can
  106.         compile and run INTVAR to see its output.  
  107.  
  108.             To  illustrate  the various ways to  output  data,  load
  109.         INTVAR2   and  observe  that  even  though  the  output   is
  110.         identical,  it  is output in a completely different  manner.
  111.         Observe  especially  that  a WRITELN all  by  itself  simply
  112.         returns to the beginning of a new line on the video monitor.
  113.         Compile and run this program also to observe its output.
  114.  
  115.                       NOW LET'S USE LOTS OF VARIABLES
  116.  
  117.             Load  ALLVAR to observe a short program using all  5  of
  118.         the  basic  data types.   The variables are simply  assigned
  119.         values and the values are printed.   A complete and detailed
  120.         description of the options available in the WRITE  statement
  121.  
  122.  
  123.                                   Page 12
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  CHAPTER 3 - The simple Pascal data types
  134.  
  135.  
  136.         is  given in the TURBO reference manual on pages 111 through
  137.         113.   It would be to your advantage to read this section at
  138.         this time since very little explanation will be given  about
  139.         WRITE  statements from this point on.   We will discuss  the
  140.         method  by which we can write to disk files or other  output
  141.         devices when the time comes.
  142.  
  143.             Back  to  the basic types.   Pascal does lots  of  cross
  144.         checking  for obvious errors.   It is illegal to assign  the
  145.         value of any variable with a value that is of the wrong type
  146.         or outside the allowable range of that variable.   There are
  147.         routines  to convert from one system to another when that is
  148.         necessary.  Suppose, for example, that you wished to use the
  149.         value of an integer in a calculation of real numbers.   That
  150.         is  possible  by first converting the integer  into  a  real
  151.         number  of the same value and using the new real variable in
  152.         the  desired calculations.   The new real variable  must  of
  153.         course  be defined in a VAR as a real before it can be used.
  154.         Details of how to do the conversion will be given later.
  155.  
  156.             Since we have some variables defined,  it would be  nice
  157.         to  use  the  properties  of computers for  which  they  are
  158.         famous, namely some mathematics.  Two programs are available
  159.         for your observation to illustrate the various kinds of math
  160.         available,  REALMATH using real variables, and INTMATH using
  161.         integer variables.   You can edit, compile, and run these on
  162.         your  own  with  no  comment from  me  except  the  comments
  163.         embedded into the source files.  Chapter 6 on pages 51 to 54
  164.         of your TURBO reference manual completely defines the simple
  165.         mathematics available.
  166.  
  167.             Byte is used just like integers but with a much  smaller
  168.         value.   Only  one byte of computer memory is used for  each
  169.         variable defined as byte but 2 are used for integer types.
  170.  
  171.                              BOOLEAN VARIABLES
  172.  
  173.             Lets  take a look at the boolean variable which is  only
  174.         allowed  to  take on two different values,  TRUE  or  FALSE.
  175.         This  variable  is  used  for loop  controls,  end  of  file
  176.         indicators  or  any other TRUE or FALSE  conditions  in  the
  177.         program.   Variables  can be compared to determine a boolean
  178.         value.  An  illustration is the best way to learn about  the
  179.         boolean variable so load BOOLMATH, observe, compile, and run
  180.         it.
  181.  
  182.             Char is a very useful variable,  but not all by  itself.
  183.         It is very powerful when used in an array or some other user
  184.         defined  data  structure which is beyond the scope  of  this
  185.         chapter.   A  very simple program,  CHARDEMO is included  to
  186.         give  you  an  idea  of how a char  variable  can  be  used.
  187.  
  188.  
  189.                                   Page 13
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                  CHAPTER 3 - The simple Pascal data types
  200.  
  201.  
  202.         Observe  and run CHARDEMO for a very brief idea of what  the
  203.         char variable is all about.
  204.  
  205.             Examine  the sample program CONVERT for several examples
  206.         of converting data from one simple variable to another.  The
  207.         program is self explanatory.
  208.  
  209.  
  210.                            PROGRAMMING EXERCISE
  211.  
  212.         1.  Write a program containing several variable  definitions
  213.             and do some math on them, printing out the results.
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.                                   Page 14
  256.